﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Studio.InternalServices;
using SoftwareConsulting.BI11.Studio.Core.Project;
using SoftwareConsulting.BI11.Studio.Core.Project.SituationsHierarchy;
using SoftwareConsulting.BI11.Studio.UI.Properties;

namespace SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.SituationsHierarchy
{
    public partial class InheritanceKindDialog : RestorableForm
    {
        private Situation _situation; //редактируемая ситуация

        /// <summary>
        /// Устанавливает текущее положение радиопереключателя
        /// </summary>
        private void Initialize()
        {
            switch (_situation.InheritanceWay)
            {
                case InheritanceKind.Range:
                    RangeRB.Checked = true;
                    break;
                case InheritanceKind.Override:
                    OverrideRB.Checked = true;
                    break;
                case InheritanceKind.Same:
                    SameRB.Checked = true;
                    break;
                case InheritanceKind.Unique:
                    UniqueRB.Checked = true;
                    break;
            }
        }

        /// <summary>
        /// Применяет изменения и закрывает диалог
        /// </summary>
        private void ApplyAndClose()
        {
            InheritanceKind bef = _situation.InheritanceWay;
            try
            {
                if (RangeRB.Checked)
                {
                    _situation.SetInheritanceWay(InheritanceKind.Range);
                }
                if (OverrideRB.Checked)
                {
                    _situation.SetInheritanceWay(InheritanceKind.Override);
                }
                if (SameRB.Checked)
                {
                    _situation.SetInheritanceWay(InheritanceKind.Same);
                }
                if (UniqueRB.Checked)
                {
                    if (GeneralMethods.ShowWarning(Resources.BI_SITUATIONSHIERARCHY_UNIQUEINHER_WARNING, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        _situation.SetInheritanceWay(InheritanceKind.Unique);
                    }
                    else
                    {
                        return;
                    }
                }
            }
            catch (BISituationsHierarchyInheritanceWayAutochangeStopException ex)
            {
                _situation.SetInheritanceWay(bef);
                GeneralMethods.ShowUserError(ex.Message);
                Initialize();
                return;
            }
            Close();
        }

        public InheritanceKindDialog(Situation situation)
        {
            _situation = situation;
            InitializeComponent();
        }

        private void InheritanceKindDialog_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            ApplyAndClose();
        }
    }
}
